package com.example.reactandroid;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import io.socket.emitter.Emitter;

/**
 * Created by Administrator on 2017/10/10.
 */

public class MyReactActivity extends ReactActivity {
    private MyService.DownLoadBinder downLoadBinder;

    private static final int  OVERLAY_PERMISSION_REQ_CODE = 1;
    @Override
    protected String getMainComponentName() {
        return "rn";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
//            intent.setData(Uri.parse("package:" + getPackageName()));
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            startActivity(intent);
//        }
        super.onCreate(savedInstanceState);
        //开启服务
//        Intent startIntent = new Intent(this, MyService.class);
//        startService(startIntent);

        SingletonSocket.getSocket().connect();

        SingletonSocket.getSocket().on("msg", onNewMessage);
    }

    private Emitter.Listener onNewMessage = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            String data = (String) args[0];

            WritableMap writableMap = new WritableNativeMap();
            writableMap.putString("key", data);

            final ReactInstanceManager reactInstanceManager =
                    getReactNativeHost().getReactInstanceManager();
            ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
            sendTransMisson(reactContext, "EventName", writableMap);
        }
    };

    public void sendTransMisson(ReactContext reactContext, String eventName, WritableMap params){

        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private ServiceConnection connection=new ServiceConnection() {
        /**
         * 服务解除绑定时候调用
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }
        /**
         * 绑定服务的时候调用
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            downLoadBinder=(MyService.DownLoadBinder) service;
            /*
             * 调用DownLoadBinder的方法实现参数的传递
             */
            downLoadBinder.startDownLoad();
            downLoadBinder.getProgress();
        }
    };
}
